
#include "Shell_command.h"
#include "common.h"

/*******************************************************************************
 * Variables
 ******************************************************************************/
static const shell_command_context_t xLedCommand = {"led",
                                                    "\r\n\"led arg1 arg2\":\r\n Usage:\r\n    arg1: 1|2|3|4...         "
                                                    "   Led index\r\n    arg2: on|off                Led status\r\n",
                                                    LedControl, 2};

static const shell_command_context_t PWMCommand = {"PWM",
                                                   "\r\n\"PWM\":\r\n   Application Adjust PWM Frequence and Duty."
                                                   "\r\n",PWM_Control, 0};

static const shell_command_context_t VL53L0X_Command = {"VL53L0X",
                                                        "\r\n\"VL53L0X\":\r\n Usage:\r\n    arg1:1|2|3...."
                                                        "\tVL53L0X index\r\n    arg2: init             VL53L0X get\r\n",
                                                        VL53L0X_Control, 0};


                                                    
/*******************************************************************************
 * Register all command
 ******************************************************************************/
void Command_Register(void)
{
	/* Add new command to commands list */
    SHELL_RegisterCommand(&xLedCommand);
    SHELL_RegisterCommand(&PWMCommand);
    SHELL_RegisterCommand(&VL53L0X_Command);

}


void SHELL_SendDataCallback(uint8_t *buf, uint32_t len)
{
    while (len--)
    {
        PUTCHAR(*(buf++));
    }
}

void SHELL_ReceiveDataCallback(uint8_t *buf, uint32_t len)
{
    while (len--)
    {
        *(buf++) = GETCHAR();
    }
}

int32_t LedControl(p_shell_context_t context, int32_t argc, char **argv)
{
    int32_t kLedIndex = ((int32_t)atoi(argv[1]));
    char *kLedCommand = argv[2];

    /* Check second argument to control led */
    switch (kLedIndex)
    {
#if defined(LED_NUMBERS) && LED_NUMBERS > 0
        case 1:
            if (strcmp(kLedCommand, "on") == 0)
            {
                LED_1_ON();
            }
            else if (strcmp(kLedCommand, "off") == 0)
            {
                LED_1_OFF();
            }
            else
            {
                SHELL_Printf("Control command is wrong!\r\n");
            }
            break;
#endif
#if defined(LED_NUMBERS) && LED_NUMBERS > 1
        case 2:
            if (strcmp(kLedCommand, "on") == 0)
            {
                LED_2_ON();
            }
            else if (strcmp(kLedCommand, "off") == 0)
            {
                LED_2_OFF();
            }
            else
            {
                SHELL_Printf("Control command is wrong!\r\n");
            }
            break;
#endif
#if defined(LED_NUMBERS) && LED_NUMBERS > 2
        case 3:
            if (strcmp(kLedCommand, "on") == 0)
            {
                LED_3_ON();
            }
            else if (strcmp(kLedCommand, "off") == 0)
            {
                LED_3_OFF();
            }
            else
            {
                SHELL_Printf("Control command is wrong!\r\n");
            }
            break;
#endif
#if defined(LED_NUMBERS) && LED_NUMBERS > 3
        case 4:
            if (strcmp(kLedCommand, "on") == 0)
            {
                LED_4_ON();
            }
            else if (strcmp(kLedCommand, "off") == 0)
            {
                LED_4_OFF();
            }
            else
            {
                SHELL_Printf("Control command is wrong!\r\n");
            }
            break;
#endif
        default:
            SHELL_Printf("LED index is wrong\r\n");
            break;
    }
    return 0;
}

int32_t PWM_Control(p_shell_context_t context, int32_t argc, char **argv)
{
    uint8_t ch;
    uint8_t duty = 0;

    while(1)
    {
        ch = GetChar(context);
		//PRINTF("%d",ch);

        if(ch == 0x3d)
        {
            PRINTF("Current duty:%d\r\n",duty);
            duty++;
        }
        if(ch == 0x2d)
        {
            PRINTF("Current duty:%d\r\n",duty);
            duty--;
        }
        if(ch == 0x1B)
        {
            PRINTF("\r\n");
            break;
        }

        /* Update duty cycles for all 3 PWM signals */
        PWM_UpdatePwmDutycycle(PWM4, kPWM_Module_0, kPWM_PwmA, kPWM_SignedCenterAligned, duty);
        PWM_UpdatePwmDutycycle(PWM4, kPWM_Module_1, kPWM_PwmA, kPWM_SignedCenterAligned, duty);
        PWM_UpdatePwmDutycycle(PWM4, kPWM_Module_2, kPWM_PwmA, kPWM_SignedCenterAligned, duty);
        PWM_UpdatePwmDutycycle(PWM4, kPWM_Module_3, kPWM_PwmA, kPWM_SignedCenterAligned, duty);

        PWM_SetPwmLdok(PWM4, kPWM_Control_Module_0 | kPWM_Control_Module_1 | kPWM_Control_Module_2 | kPWM_Control_Module_3, true);
    }
    
    return 0;
}

int32_t VL53L0X_Control(p_shell_context_t context, int32_t argc, char **argv)
{
    uint8_t ch;

    PRINTF("Press any key to get current Distance. ESC to quit.\r\n",Distance);

    while(1)
    {
        ch = GetChar(context);
        PRINTF("current distance:%d\t%d\t%d\t%d\t\r\n",Distance,Dis_State,Dis_ambient,Dis_signal);
        TaskDelay_ms(200);
        if(ch == 0x1B)
        {
            PRINTF("\r\n");
            break;
        }
    }
    
    return 0;
}

